#!/bin/bash
#
# fxphp     This shell script takes care of starting and stopping
#               fxphp
#
#
# chkconfig: 2345 99 20

# Source function library.
. /etc/init.d/functions

getpid() {
  echo `ps -ef|grep -w $COMMAND|grep -v grep|awk '{printf $2}'`
}

FXPHP_DIR=/var/lib/foxit_ucx
PIDFILE=/var/run/fxphp.pid
prog=fxphp
COMMAND="./sendmail"
PID=`getpid`

if [ -z $PID ] && [ -e ${PIDFILE} ]; then
  rm ${PIDFILE}
fi

if [ -z $PID ] && [ -e /var/lock/subsys/$prog ]; then
  rm -f /var/lock/subsys/$prog
fi

start() {


	PID=`getpid`

	if [ $PID ]; then
                echo Foxit PHP Service already running.
        else

                cd $FXPHP_DIR
                ./sendmail &
		echo $! > ${PIDFILE}
                RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
	    	  action $"Starting $prog: " /bin/true
	    	  touch /var/lock/subsys/$prog
		else
	    	  action $"Starting $prog: " /bin/false
		fi
                return $RETVAL
        fi

}

stop() {
	PID=`getpid`
	if [ -z $PID ]; then
	  echo "Foxit PHP Service already stopped"
	else
          kill -9 $PID
          action $"Stopping $prog: " /bin/true
	fi
        rm -f /var/lock/subsys/$prog ${PIDFILE}
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${PIDFILE} $prog
        ;;
    restart)
        stop
	sleep 5
        start
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $?

